<?php
include('admin.php');
set_page_info('id', 'uploads');

if (ACTION == 'new') {
	if (isset($_POST['submit'])) {
		$c = get_post_data();
		
		if (empty($_FILES) || !is_array($_FILES) || empty($_FILES['image']) || !is_array($_FILES['image']) || (int)$_FILES['image']['error'] === 4) error('Please select a file to upload.');
		
		$c['file'] = $_FILES['image'];
		$c['date'] = time() - (60 * 60 * (int)get_option('timeoffset'));
		$year = get_date('Y');
		$month = get_date('m');
		$uploadsDir = ABSPATH.'uploads/';
		
		if (!is_dir($uploadsDir.$year)) mkdir($uploadsDir.$year);
		if (!is_dir($uploadsDir.$year.'/'.$month)) mkdir($uploadsDir.$year.'/'.$month);
		$c['name'] = $year.'/'.$month.'/'.$c['file']['name'];
		
		$move = move_uploaded_file($c['file']['tmp_name'], $uploadsDir.$c['name']);
		if ($move !== false) {
			$result = $mysql->insert(UPLOADS, array(
				'upload_date' => get_date($dateFormats['mysql'], $c['date']),
				'upload_url' => $c['name']
			));
			
			if ($result === true) header('Location: uploads.php#upload-'.$mysql->lastInsertId);
			else error('There was an error saving the image.');
		} else error('There was an error uploading the image.');
	} else {
		set_page_info('title', 'New Upload');
		get_header();
?>
		<h2>New Upload</h2>
		<form action="uploads.php?action=new" method="post" enctype="multipart/form-data">
			<p><label for="image">Image</label>
			<input type="file" name="image" id="image" /></p>
			<p>
				<input type="hidden" name="MAX_UPLOAD_SIZE" value="1073741824" /> <!--1GB-->
				<input type="submit" name="submit" class="button" value="Save" />
			</p>
		</form>
<?php
		get_footer();
	}
} elseif (ACTION == 'edit') {
	$id = (int)get_global($_GET, 'id');
	$upload = get_single_item(array(
		'table' => UPLOADS,
		'class' => 'upload',
		'where' => '`upload_id` = "'.$id.'"'
	));
	if (empty($upload)) error('That is not a valid upload.');
	
	if (isset($_POST['submit'])) {
		$c = get_post_data();
		
		if (empty($_FILES) || !is_array($_FILES) || empty($_FILES['image']) || !is_array($_FILES['image']) || (int)$_FILES['image']['error'] === 4) error('Please select a file to upload.');
		
		$c['file'] = $_FILES['image'];
		$year = get_date('Y');
		$month = get_date('m');
		$uploadsDir = ABSPATH.'uploads/';
		
		if (!is_dir($uploadsDir.$year)) mkdir($uploadsDir.$year);
		if (!is_dir($uploadsDir.$year.'/'.$month)) mkdir($uploadsDir.$year.'/'.$month);
		$c['name'] = $year.'/'.$month.'/'.$c['file']['name'];
		
		$move = move_uploaded_file($c['file']['tmp_name'], $uploadsDir.$c['name']);
		if ($move !== false) {
			@unlink($uploadsDir.$upload->url);
			$result = $mysql->update(UPLOADS, array(
				'upload_url' => $c['name']
			), '`upload_id` = "'.$upload->get_id().'"');
			
			if ($result === true) header('Location: uploads.php#upload-'.$mysql->lastInsertId);
			else error('There was an error saving the image.');
		} else error('There was an error uploading the image.');
	} else {
		set_page_info('title', 'Editting Upload: #'.$upload->get_id());
		get_header();
?>
		<h2>Editting Upload: #<?php $upload->the_id(); ?></h2>
		<form action="uploads.php?action=edit&amp;id=<?php echo $id; ?>" method="post" enctype="multipart/form-data">
			<p><label>Current Image</label>
			<?php $upload->the_image(true, array('class'=>'upload', 'link'=>true)); ?></p>
			<p><label for="image">Image</label>
			<input type="file" name="image" id="image" /></p>
			<p><label>URL</label>
			<?php $upload->the_image(false); ?></p>
			<p>
				<input type="hidden" name="MAX_UPLOAD_SIZE" value="1073741824" /> <!--1GB-->
				<input type="submit" name="submit" class="button" value="Save" />
			</p>
		</form>
<?php
		get_footer();
	}
} elseif (ACTION == 'delete') {
	$id = (int)get_global($_GET, 'id');
	$result = delete_upload($id);
	
	if ($result === true) header('Location: uploads.php');
	else error('There was an error deleting the upload.');
} else {
	$uploads = new items(array(
		'table' => UPLOADS,
		'class' => 'upload',
		'order' => '`upload_id` DESC'
	));
	set_page_info('title', 'Manage Uploads');
	get_header();
?>
		<h2>Manage Uploads<span>(<a href="uploads.php?action=new">add one?</a>)</span></h2>
		<form action="mass-delete.php?type=uploads" method="post">
			<table id="uploads"  width="100%">
				<thead>
					<tr>
						<th><input type="checkbox" /></th>
						<th>Image</th>
						<th>Date</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="4">
							<input type="submit" name="submit" class="button" value="Delete" />
							Page: <?php echo $uploads->pagination['page']; ?><?php if ($uploads->needsPagination()) { echo ' &mdash; '; $uploads->pagination(); } ?>
						</td>
					</tr>
				</tfoot>
				<tbody>
<?php
	if ($uploads->total > 0) {
		foreach($uploads->items as $upload) {
?>
					<tr<?php $upload->the_row_info(); ?>>
						<td class="checkbox"><input type="checkbox" name="mass_delete[]" value="<?php $upload->the_id(); ?>" /></td>
						<td><a href="<?php $upload->the_image(false); ?>"><?php $upload->the_image(true, array('class'=>'upload')); ?></a></td>
						<td><?php $upload->the_date('F j, Y'); ?></td>
						<td class="options"><?php $upload->the_options(); ?></td>
					</tr>
<?php
		}
	} else {
?>
					<tr>
						<td colspan="4">No uploads found.</td>
					</tr>
<?php } ?>
				</tbody>
			</table>
		</form>
<?php
	get_footer();
}
?>